package App.Controllers;

import App.Entity.Auth;
import App.Entity.Notice;
import App.Entity.Project;
import App.Entity.Task;
import App.Filter.AuthInterceptor;
import App.Service.ProjectService;
import App.Utils.PoJo.Error;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import App.Utils.PoJo.*;
/**
 * Created by yu on 2017/7/19.
 */
@RestController
@RequestMapping("/project")
public class ProjectController
{
    @Autowired
    ProjectService projectService;
    public static String projectNoeExistError="ProjectNotExistError";
    public static String authError="AuthError";

    @RequestMapping("/addProject")
    public Object addProject(Project project)
    {
        Project project1=projectService.createProject(project);
        return new Success("添加项目成功",project1);
    }

    @RequestMapping("/addTask")
    public Object addTask(Task task)
    {
        if(!projectService.existProject(task.getProjectId()))
        {
            return new Error("该项目不存在",projectNoeExistError);
        }
        if(!projectService.canAddTask(task.getProjectId(),task.getPublisher()))
        {
            return new Error("你没有权限添加任务",authError);
        }
        projectService.addTask(task);
        return new Success("添加任务成功",task);
    }

    @RequestMapping("/getTasks")
    public Object getProjectTasks(@RequestParam("projectId") Long ProjectId)
    {
        if(!projectService.existProject(ProjectId))
        {
            return new Error("该项目不存在",projectNoeExistError);
        }
        return new Success("获取任务成功",projectService.getTask(ProjectId));
    }

    @RequestMapping("/receiveTask")
    public Object receiveTask(@RequestParam("projectId") Long ProjectId,@RequestParam("receiverId") Long ReceiverId,@RequestParam("taskId") Long TaskId)
    {
        if(!projectService.existProject(ProjectId))
        {
            return new Error("该项目不存在",projectNoeExistError);
        }
//        if(!projectService.inProject(ProjectId, ReceiverId))
//        {
//            return new Error("你不在该项目中",authError);
//        }
        if(!projectService.waitForReceive(TaskId))
        {
            return new Error("该任务已被领取","TaskNotAbleToReceiveError");
        }
        projectService.giveTask(TaskId,ReceiverId,ProjectId);
        return new Success("成功领取");
    }

    @RequestMapping("/joinProject")
    public Object joinProject(@RequestParam("projectId")Long ProjectId,@RequestParam("authId")Long authId)
    {
        if(!projectService.existProject(ProjectId))
        {
            return new Error("该项目不存在",projectNoeExistError);
        }
        if(projectService.inProject(ProjectId,authId))
        {
            return new Error("你已经在项目中了","ExistError");
        }
        projectService.joinProject(ProjectId,authId);
        return new Success("参与项目成功");
    }

    @RequestMapping("/inProject")
    public Object inProject(@RequestParam("projectId") Long projectId,@RequestParam("authId") Long authId){
        return new Success(true,projectService.inProject(projectId,authId));
    }


    @RequestMapping("/finishTask")
    public Object finishTask(@RequestParam("taskId") Long taskId,@RequestParam("authId") Long authId)
    {
        projectService.finishTask(taskId, authId);
        return new Success("完成任务成功");
    }

    @RequestMapping("/getPersonTask")
    public Object getTasksOfProject(@RequestParam("authId") Long authId,@RequestParam("projectId") Long projectId)
    {
        return new Success("获取项目任务成功",projectService.getPersonTask(projectId,authId));
    }

    @RequestMapping("/getProgress")
    public Object getProjectProgress(@RequestParam("projectId") Long ProjectId)
    {
        return new Success("获取项目进度成功",projectService.getProgressOfProject(ProjectId));
    }

    @RequestMapping("/publishNotice")
    public Object publishNotice(Notice notice)
    {
        projectService.publishNotice(notice);
        return new Success(true,true);
    }

    @RequestMapping("/getRunningTask")
    public Object getRunningTask(@RequestParam("projectId")Long ProjectId)
    {
        return new Success(true,projectService.getRunningTaskOfProject(ProjectId));
    }

    @RequestMapping("/getFinishTask")
    public Object getFinishTask(@RequestParam("projectId") Long ProjectId)
    {
        return new Success(true,projectService.getFinishTaskOfProject(ProjectId));
    }

    @RequestMapping("/getUpdatedTask")
    public Object getUpdatedTask(@RequestParam("projectId")Long projectId,@RequestParam("modifyTime")Long modifyTime){
        return new Success(true,projectService.getUpdatedTaskByModifyTime(projectId,modifyTime));
    }


    @RequestMapping("/getFinishTaskByPage")
    public Object getFinishTask(@RequestParam("projectId")Long ProjectId,@RequestParam("modifyTime")long modifyTime){
        return new Success(true,projectService.getFinishTaskOfProjectByPage(ProjectId,modifyTime));
    }

    @RequestMapping("/getReceivingTask")
    public Object getReceivingTask(@RequestParam("projectId") Long ProjectId)
    {
        return new Success(true,projectService.getReceiveTask(ProjectId));
    }

    @RequestMapping("/getUpdatedReceivingTask")
    public Object getUpdateReceivingTask(@RequestParam("projectId")Long projectId,@RequestParam("publicTime")Long publicTime){
        return new Success(true,projectService.getUpdatedReceivingTaskByPublicTime(projectId,publicTime));
    }

    @RequestMapping("/person/getRunningTask")
    public Object getRunningTaskOfPerson(@RequestParam("projectId")Long projectId,@RequestParam("authId")Long authId)
    {
        return new Success(true,projectService.getPersonRunningTask(projectId,authId));
    }

    @RequestMapping("/person/getFinishedTask")
    public Object getFinishedTaskOfPerson(@RequestParam("projectId")Long projectId,@RequestParam("authId")Long authId)
    {
        return new Success(true,projectService.getPersonFinishedTask(projectId,authId));
    }

    @RequestMapping("/cancelFinishTask")
    public Object cancelFinishTask(@RequestParam("taskId")Long taskId,@RequestParam("authId")Long authId)
    {
        projectService.cancelFinishTask(taskId, authId);
        return new Success(true,true);
    }

    @RequestMapping("/person/newNotice")
    public Object getNewNotice(@RequestParam("projectId")Long projectId,@RequestParam("authId")Long authId)
    {
        return new Success(true,projectService.getNewNotice(projectId,authId));
    }

    @RequestMapping("/person/ReadNotice")
    public Object getReadNotice(@RequestParam("projectId")Long projectId,@RequestParam("authId")Long authId)
    {
        return new Success(true,projectService.getReadNotice(projectId,authId));
    }

    @RequestMapping("/person/getNotices")
    public Object getNotices(@RequestParam("projectId")Long projectId,@RequestParam("authId")Long authId){
        return new Success(true,projectService.getNotices(projectId,authId));
    }

    @RequestMapping("/contact/getInfo")
    public Object getMatesInfo(@RequestParam("projectId") Long projectId)
    {
        return new Success(true,projectService.getMatesWithProgress(projectId));
    }

    @RequestMapping("/contact/getAllContact")
    public Object getProjectContact(@RequestParam("projectId")Long projectId,@RequestParam("authId") Long authId){
        return new Success(true,projectService.getProjectContact(projectId,authId));
    }

















}
